<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;

class SearchController extends HomeBaseController
{
    public function index()
    {
        $keyword = $this->request->param('keyword');
        // $type    = $this->request->param('type',1,'intval');

        // if (empty($keyword)) {
        //     $this -> error("关键词不能为空！请重新输入！");
        // }

        /*//基地文章总记录数
        $field = 'post.id,post.post_title,post.published_time,category_post.category_id,category.name';
        $where = [
            'category_post.is_show' => 1,
            'post.post_status'      => 99,
            'post.create_time'      => ['egt',0],
            'post.delete_time'      => 0,
            'post.published_time'   => [['> time', 0], ['<', time()]],
        ];
        
        $likeWhere = "post.post_title like '%$keyword%' or post.post_keywords like '%$keyword%' or post.post_content like '%$keyword%'";

        $join = [
            ['__BRANCH_CATEGORY_POST__ category_post', 'post.id = category_post.post_id'],
            ['__BRANCH_CATEGORY__ category', 'category_post.category_id = category.id']
        ];
        $branchCount = model('branchPost')->alias('post')->field($field)
            ->join($join)
            ->where($where)
            ->where($likeWhere)
            ->count();

        //文章总记录数
        $where['post.post_type'] = 1;
        $join = [
            ['__PORTAL_CATEGORY_POST__ category_post', 'post.id = category_post.post_id'],
            ['__PORTAL_CATEGORY__ category', 'category_post.category_id = category.id']
        ];
        $articleCount = model('PortalPost')->alias('post')->field($field)
            ->join($join)
            ->where($where)
            ->where($likeWhere)
            ->count();

        $goodsLikeWhere = "g.goods_name like '%$keyword%' or g.post_keywords like '%$keyword%' or g.charac like '%$keyword%'";
        //产品总记录数
        $goodsCount = db('goods')
              -> alias("g")
              -> join("__GOODS_CATEGORY_RELATE__ category_relate", "g.goods_id = category_relate.goods_id",'LEFT')
              -> join('__GOODS_CATEGORY__ category', 'category_relate.category_id = category.id')
              -> where($goodsLikeWhere)
              -> where('category_relate.is_show',1)
              -> where('g.delete_time',0)
              -> count();

        $this -> assign("article_count", $articleCount);
        $this -> assign("branch_count", $branchCount);
        $this -> assign("goods_count", $goodsCount);*/
        // $this -> assign("type", $type);
        
        $this -> assign("keyword", $keyword);

        return $this->fetch('/search');
    }
}
